$(function () {
         // 所有的ajax form提交,由于大多业务逻辑都是一样的，故统一处理

     
    var ajaxForm_list = $('form.js-ajax-form');
    if (ajaxForm_list.length) {

        Wind.use('ajaxForm', 'artDialog', function () {
         

            $('button.js-ajax-submit').on('click', function (e) {
                e.preventDefault();
                /*var btn = $(this).find('button.js-ajax-submit'),
                    form = $(this);*/
                var btn = $(this),
                    form = btn.parents('form.js-ajax-form');
                
                if(btn.data("loading")){
                    return;
                }

                //批量操作 判断选项
                if (btn.data('subcheck')) {
                    btn.parent().find('span').remove();
                    if (form.find('input.js-check:checked').length) {
                        var msg = btn.data('msg');
                        if (msg) {
                            art.dialog({
                                id: 'warning',
                                icon: 'warning',
                                content: btn.data('msg'),
                                cancelVal: '关闭',
                                cancel: function () {
                                    //btn.data('subcheck', false);
                                    //btn.click();
                                },
                                ok: function () {
                                     btn.data('subcheck', false);
                                     btn.click();
                                }
                            });
                        } else {
                            btn.data('subcheck', false);
                            btn.click();
                        }

                    } else {
                        $('<span class="tips_error">请至少选择一项</span>').appendTo(btn.parent()).fadeIn('fast');
                    }
                    return false;
                }

              
                
                form.ajaxSubmit({
                    url: btn.data('action') ? btn.data('action') : form.attr('action'), //按钮上是否自定义提交地址(多按钮情况)
                    dataType: 'json',
                    beforeSubmit: function (arr, $form, options) {
                        
                        btn.data("loading",true);
                        var text = btn.text();

                        //按钮文案、状态修改
                        btn.text(text + '中...').prop('disabled', true).addClass('disabled');
                    },
                    success: function (data, statusText, xhr, $form) {
                        var text = btn.text();

                        //按钮文案、状态修改
                        btn.removeClass('disabled').text(text.replace('中...', '')).parent().find('span').remove();
                        if (data.state === 1) {
                            $('<span class="tips_success">' + data.info + '</span>').appendTo(btn.parent()).fadeIn('slow').delay(1000).fadeOut(function () {
                            });
                        } else if (data.state === 0) {
                            var $verify_img=form.find(".verify_img");
                            if($verify_img.length){
                                $verify_img.attr("src",$verify_img.attr("src")+"&refresh="+Math.random()); 
                            }
                            
                            var $verify_input=form.find("[name='verify']");
                            $verify_input.val("");
                            
                            $('<span class="tips_error">' + data.info + '</span>').appendTo(btn.parent()).fadeIn('fast');
                            btn.removeProp('disabled').removeClass('disabled');
                        }
                        
                        if (data.referer) {
                            //返回带跳转地址
                            if(window.parent.art){
                                //iframe弹出页
                                window.parent.location.href = data.referer;
                            }else{
                                window.location.href = data.referer;
                            }
                        } else {
                            if (data.state === 1) {
                                if(window.parent.art){
                                     location.reload();
                                }else{
                                    //刷新当前页
                                    location.reload();
                                }
                            }
                        }
                        
                    },
                    complete: function(){
                         location.reload();
                    }
                });
            });

        });
    }
         //所有的删除操作，删除数据后刷新页面
    if ($('a.js-ajax-delete').length) {

        Wind.use('artDialog', function () {
            $('.js-ajax-delete').on('click', function (e) {

                e.preventDefault();
                var $_this = this,
                    $this = $($_this),
                    href = $this.prop('href'),
                    msg = $this.data('msg');
                art.dialog({
                    title: false,
                    icon: 'question',
                    content: '确定要删除吗？',
                    follow: $_this,
                    close: function () {
                        $_this.focus();; //关闭时让触发弹窗的元素获取焦点
                        return true;
                    },
                    okVal:"确定",
                    ok: function () {
                        
                        $.getJSON(href).done(function (data) {
                            console.log(data);
                            if (data.status === 1) {
                                if (data.referer) {
                                    location.href = data.referer;
                                } else {
                                   location.reload();
                                }
                            } else if (data.status === 0) {
                                
                                art.dialog({   
                                    content: data.info,
                                    icon: 'warning',  
                                    ok: function () {   
                                        this.title(data.info);   
                                        return true;   
                                    }
                                }); 
                            }
                        });
                    },
                    cancelVal: '关闭',
                    cancel: true
                });
            });

        });
    }
})


//checkbox全选/取消
$.fn.setAllCheckbox=function(option){
    var $this=$(this);
    var dft={
        callFunc:$.noop
    }
    var Options=$.extend(true,dft,option);
    var $this=$(this);
    //找到全选操作的checkbox
    var checkAllinput=$(this).find('.check-all');
    checkAllinput.click(function(){
        var _self=$(this);
        var checkName=$(this).attr('data-target');
        var checkItem=$this.find(':checkbox[data-name="'+checkName+'"]');

        checkItem.prop('checked',$(this).prop('checked'));
        checkAllinput.prop('checked',$(this).prop('checked'));
        Options.callFunc.apply($(this));
    });
}
function showMessage(type, message,url,time)
        {
            if(url == undefined){
                url = '';
            }
            if(time == undefined){
                time = 3;
            }
            //成功之后的跳转
            if(type == 'success')
                {
                $( "#dialog" ).dialog({             
                    buttons: {
                        "确定,#51A351": function() {
                            $(this).dialog('close');
                        }
                    },
                    contentText:message,
                    time:time,
                    timeHref: url,
                });
                }
            //失败之后的跳转
            if(type == 'error')
                {
                $( "#dialog" ).dialog({             
                    buttons: {
                        "确定,#e57373": function() {
                            $(this).dialog('close');
                        }
                    },
                    time:time,
                    contentText:message,
                    timeHref: url,
                });
                }
            
        }
        function showConfirm(content)
        {
            $( "#dialog" ).dialog({             
                buttons: {
                    "确定,#e57373": function() {
                        $(this).dialog('close');
                        return 1;
                    },
                    "取消": function() {
                        $(this).dialog('close');
                        return 0;
                    }
                },
                contentText:content,
            });
            
        }


//按数组对象中的某个键值(需为数字)进行插入排序
function insertSort(sortObj,key,saveOldKey,orderby){
    var temp;
    $.each(sortObj,function(index,Item){
        temp=sortObj[index+1];
        if(temp){
            temp.oldKey=index+1;
            for(var i=index;i>=0;i--){
                //console.log(sortObj[i]);
                if(orderby=='asc' || !orderby){
                    if(parseInt(temp[key])<parseInt(sortObj[i][key])){
                        sortObj[i+1]=sortObj[i];
                        if(saveOldKey==true){
                            if(sortObj[i+1].oldKey==undefined){
                                sortObj[i+1].oldKey=i;
                            }
                        }
                    }else{
                        break;
                    }
                }else if(orderby=='desc'){
                    if(parseInt(temp[key])>parseInt(sortObj[i][key])){
                        sortObj[i+1]=sortObj[i];
                        if(saveOldKey==true){
                            if(sortObj[i+1].oldKey==undefined){
                                sortObj[i+1].oldKey=i;
                            }
                        }
                    }else{                      
                        break;
                    }
                }
            }
            if((i+1)!==index+1){
                sortObj[i+1]=temp;
            }else{
                if(saveOldKey==true){
                    if(sortObj[i].oldKey==undefined){
                        sortObj[i].oldKey=i;
                    }
                    if(sortObj[i+1].oldKey==undefined){
                        sortObj[i+1].oldKey=i+1;
                    }
                }
            }
        }
    })
    return sortObj;
}